package com.memora.seguranca

class SegUser {

	transient springSecurityService
	
	String	email
	String 	sobremim
	Date 	dataNascimento	

	String nome
	String sobrenome
	String sexo
	boolean primeiroAcesso = true
	String avatar = "default"

	String username
	String password
	boolean enabled = true
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

	static transients = ['springSecurityService']

	static constraints = {
		nome		blank: false
		sobrenome	blank: false
		sexo		blank: false
		username 	blank: false, unique: true
		password 	blank: false
		email	 	email: true, blank: false
	}

	static mapping = {
		password column: '`password`'
	}

	Set<SegRole> getAuthorities() {
		SegUserSegRole.findAllBySegUser(this).collect { it.segRole } as Set
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
/*
	String toString(){

		return (this.nome + " " + this.sobrenome).substring(0, 10) + "..."

	}
*/
}
